﻿/**************************
 * Author: Khangnv6b
 * Date: 28/03/2013
 * Module: Manage Group
 * Project: OFB
 **************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using OFB.Domain;
using System.Data;
using System.Data.SqlClient;

namespace OFB.BLL.Repository
{

    /// <summary>
    /// Class contain all BLL of Manage Group module
    /// </summary>
    public class GroupBLL : IGroupBLL
    {
        private GroupContext context;
        /// <summary>
        /// Initial Context
        /// </summary>
        public GroupBLL()
        {
            context = new GroupContext();
        }


        public bool IsUsed(int groupId)
        {
            IDataParameter rowEffected = context.Group.BuildParameter("RowEffect", DbType.Int32, ParameterDirection.Output);

            context.Group.Execute("Group_IsUsed", context.Group.BuildParameter("GroupId", DbType.Int32, groupId),

                                                         rowEffected);
            if ((int)rowEffected.Value == 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// This method performs finding all Group
        /// </summary>
        /// <returns></returns>
        public ICollection<Group> FindAll(int startRow, int maxRow, out int totalRow)
        {
            IDataParameter para = context.Group.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<Group> lst = context.Group.Init("Group_Filter", context.Group.BuildParameter("startRowIndex", DbType.Int32, startRow),
                                     context.Group.BuildParameter("maximumRows", DbType.Int32, maxRow),
                                     para)
                                               .MapCollection<Group>()
                                               .GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }


        /// <summary>
        /// This method performs filter Group
        /// </summary>
        /// <param name="status"></param>
        /// <param name="totalRow"> </param>
        /// <param name="groupName"></param>
        /// <param name="groupId"></param>
        /// <param name="searchString"></param>
        /// <param name="startRow"> </param>
        /// <param name="maxRow"> </param>
        /// <returns></returns>
        public ICollection<Group> Filter(bool? status, int startRow, int maxRow, out int totalRow, string groupName = null, string groupId = null, string searchString = null)
        {
            IDataParameter para = context.Group.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<Group> lst = context.Group.Init("Group_Filter",
                                      context.Group.BuildParameter("Status", DbType.Boolean, status),
                                      context.Group.BuildParameter("GroupName", DbType.String, groupName),
                                      context.Group.BuildParameter("GroupId", DbType.Int32, groupId),
                                      context.Group.BuildParameter("SearchString", DbType.Int32, searchString),
                                      context.Group.BuildParameter("startRowIndex", DbType.Int32, startRow),
                                      context.Group.BuildParameter("maximumRows", DbType.Int32, maxRow),
                                      para)
                                 .MapCollection<Group>()
                                 .GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }


        /// <summary>
        /// This method performs creating group
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="description"> </param>
        public int CreateGroup(string groupName, string description)
        {
            IDataParameter groupIdParam =
                context.Group.BuildParameter("GroupId", DbType.Int32, ParameterDirection.Output);

            context.Group.Execute("Group_Create", context.Group.BuildParameter("GroupName", DbType.String, groupName),
                                            context.Group.BuildParameter("Description", DbType.String, description),
                                            groupIdParam);
            return (int)groupIdParam.Value;
        }


        /// <summary>
        /// This method performs get data of Group by groupId
        /// </summary>
        /// <param name="groupId"></param>
        public Group GetByGroupId(int groupId)
        {
            return context.Group.Init("Group_GetByGroupId",
                                     context.Group.BuildParameter("GroupId", DbType.Int32, groupId))
                                     .Map()
                                     .Get();
        }


        /// <summary>
        /// This method performs updating Group
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="groupName"> </param>
        /// <param name="description"> </param>
        public int UpdateGroup(int groupId, string groupName, string description)
        {
            IDataParameter rowEffected =
                context.Group.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);

            context.Group.Execute("Group_Update", context.Group.BuildParameter("GroupId", DbType.UInt32, groupId),
                                            context.Group.BuildParameter("GroupName", DbType.String, groupName),
                                            context.Group.BuildParameter("Description", DbType.String, description),
                                            rowEffected);
            return (int)rowEffected.Value;
        }


        /// <summary>
        /// This method performs changing status of Group
        /// </summary>
        /// <param name="groupId"></param>
        public int ChangeStatus(int groupId)
        {
            IDataParameter rowEffected = context.Group.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.Group.Execute("Group_ChangeStatus", context.Group.BuildParameter("GroupId", DbType.UInt32, groupId),
                                  rowEffected);
            return (int)rowEffected.Value;
        }


        /// <summary>
        /// This method performs searching Group by Group Name
        /// </summary>
        /// <param name="groupId"></param>
        public Group SearchByGroupName(string groupName)
        {
            return context.Group.Init("Group_SearchByGroupName",
                                        context.Group.BuildParameter("GroupName", DbType.String, groupName))
                                        .Map()
                                        .Get();
        }



        //vinhnq
        public ICollection<Group> Retrieve()
        {
            return context.Group.Init("Group_Retrieve")
                                .MapCollection<Group>()
                                .GetCollection();
        }

    }
}
